/* 
    对mysql操作进行封装
*/
const mysql = require("mysql");

function sqlQuery(sql, success, fail) {
  const connection = mysql.createConnection({
    //开发环境
    host: "localhost",
    user: "root",
    password: "xhy189683",
    database: "xhy_blog",

    /*  //正式环境
         host: '118.178.182.26',
         user: 'xhy_data',
         password: 'FpWXx5PSJZ26BsEh',
         database: 'xhy_data' */
  });
  connection.connect();
  connection.query(sql, (err, data) => {
    if (err) {
      console.warn("sql报错");
      fail(err);
      // throw err;
    }
    success(data);
  });
  closeMysql(connection);
}
function closeMysql(connection) {
  connection.end((err) => {
    if (err) {
      console.log("数据库关闭失败" + err);
    } else {
      console.log("断开连接成功");
    }
  });
}

module.exports = { sqlQuery };
